package com.cetcs.kmga.log.util;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import com.alibaba.druid.pool.DruidPooledConnection;

import java.io.InputStream;
import java.sql.SQLException;
import java.util.Properties;

/**
 * Created by XingWen on 2017/5/13.
 */
public class MySQLDruidPool {
    private static MySQLDruidPool dbPoolConnection = null;
    private static DruidDataSource druidDataSource = null;
    private  static String MYSQL_PATH = "conf/druid-mysql.properties";

    static {
        try {
            InputStream inputStream = MySQLDruidPool.class.getClassLoader().getResourceAsStream(MYSQL_PATH);
            Properties p = new Properties();
            p.load(inputStream);
            druidDataSource = (DruidDataSource)DruidDataSourceFactory.createDataSource(p); //DruidDataSrouce工厂模式
            inputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static synchronized MySQLDruidPool getInstance(){
        if (null == dbPoolConnection){
            dbPoolConnection = new MySQLDruidPool();
        }
        return dbPoolConnection;
    }

    public DruidPooledConnection getConnection() throws SQLException{
        return druidDataSource.getConnection();
    }




}
